log = console.log;
/*
*---------------------------------------------------------------
* APPLICATION ENVIRONMENT
*---------------------------------------------------------------
*
* You can load different configurations depending on your
* current environment. Setting the environment also influences
* things like logging and error reporting.
*
* This can be set to anything, but default usage is:
*
*     development
*     testing
*     production
*
* NOTE: If you change these, also change the error_reporting() code below
*
*/
var myConfig = [
    {
        ENVIRONMENT: 'development',
        SHOW_TESTCASES:true
    }
]
var myObject = [
    {
        ID:null,
        mode:'NEW',
        type:'apartment',
        formViewUrl: base_url+'objects/build_form',
        formEditUrl: base_url+'objects/build_form',
        hasDeleteHandler:null
    }
]
var myOwner = [
    {
        ID:null,
        mode:'NEW',
        type:'company',
        formViewUrl: base_url+'owners/view_new',
        formEditUrl: base_url+'owners/view_edit/',
        hasHandlers:null
    }
]

/*
* Alert html helper method
* 
* @param string
* @param type (info, success, error) 
*/ 
function alert_html(string,type)
{
    html = '<div class="alert alert-'+type+' clearfix">'
    '<button type="button" class="close" data-dismiss="alert">&times;</button>'
    html+= string; 
    html+= '</div>';
    return html;  
}

/*
* Loader html helper method
* 
*/ 
function loader_html()
{
    return '<img src="'+base_url+'assets/img/backgnds/loading.gif">';
} 

/*
* Populate edit object form on dropdown changed
* 
* @param type ( apartment, hotel, villa )
* @param url form URL
* @param id Object ID if supplied, populate fields
* @param id buttonToEnable id of button to enables fro form submit
*/
function get_object_fields(type,url,id,formbutton)
{       
    var data
    if(id!=null)
        {
        data = {type: type, id: id}; 
    }else
        {
        data = {type: type};
    }

    $.ajax({
            url: url,
            dataType: 'json',
            type: "POST",                              
            data: data,
            success: function (data, textStatus, xhr) {

                $('#data-container').html(data.html);

                $(formbutton).removeAttr("disabled");
                //test code Add Object
                /*$('#name').val('New Object Name Test');
                $('#numberOfStars').val('5 stars');
                $('#distanceFromTheSea').val('400 m');
                $('#distanceFromTheCenter').val('1 km');
                $('#editObjectButton').trigger('click');*/

            },
            error: function (xhr, textStatus, errorThrown) {
                alert('Error ocured...\n'+xhr+'\n'+textStatus+errorThrown+'\n');
            }
    });  
}

/*
* Fill object forms with updated fileds
* 
*/
function get_object_updated_fields_helper()
{
    // Fill forms with new fields
    switch (myObject[0]['mode']) {
        case 'NEW':
            get_object_fields(
                myObject[0]['type'],
                myObject[0]['formViewUrl'],
                myObject[0]['ID']
            );
            break;
        case 'EDIT':
            get_object_fields(
                myObject[0]['type'],
                myObject[0]['formEditUrl'],
                myObject[0]['ID']
            );
            break;
        default:
            alert('myObject.mode is undefiends')
    }
}


/*
* Edit Object Dialog
* 
* @param urlToOpen
* @param dialogClass
* @param dialogTitle
* @param buttons collection
* @param DialogOnOpen
* @param width
* @param heigh
* 
*/
newDialog = function(urlToOpen,dialogClass,dialogTitle,buttons,DialogOnOpen,width,height){  

    var tag = $("<div class='"+dialogClass+"'></div>");

    $.ajax({
            url: urlToOpen,
            // Disable caching of AJAX responses
            cache: false,
            success: function(data) { 

                tag.html(data).dialog({
                        height: height,
                        width: width,
                        modal: true,
                        title: dialogTitle,
                        buttons:buttons,                        
                        open: DialogOnOpen,
                        close: function() {
                            $(this).dialog("destroy").remove();
                        }
                }).dialog('open');
            }
    });
}

//DONE:programaticly select radio
select_owner_type = function(type)
{
    $('#radio').buttonset();
    switch(type)
    {
        case 'company':
            $('.cg-company_ID').show();
            $('.cg-personal_ID').hide();
            $('.cg-birthdate').hide();
            $('.cg-pasport_number').hide();
            $('.cg-sex').hide();
            $('.cg-surname').hide();
            $('.cg-birthplace').hide();
            $('.cg-citizenship').hide();
            $('[name="company_person"][value="company"]').attr("checked", true); 

            //log('company')
            break;

        case 'person':
            $('.cg-company_ID').hide();
            $('.cg-personal_ID').show();
            $('.cg-birthdate').show();
            $('.cg-pasport_number').show();
            $('.cg-sex').show();
            $('.cg-surname').show();
            $('.cg-birthplace').show();
            $('.cg-citizenship').show();
            $('#company_person-person').attr('checked', true);
            $('[name="company_person"][value="person"]').attr("checked", true); 

            //log('person')
            break;
    }

    $('#radio').buttonset("refresh");

}

$(document).ready(function() { 
        //check dropdown methot - valdation
        jQuery.validator.addMethod("check_item_dropdown", function(value, element) {  
                return this.optional(element) || value != 0  ;   
            }, "Please select an item from the dropdown list.");

        // Override default error message
        jQuery.validator.messages.required = "";

});